package com.rock.upms.web.system.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.rock.common.base.BaseController;
import com.rock.common.base.BaseQuery;
import com.rock.common.base.BaseResult;
import com.rock.common.util.StringUtil;
import com.rock.upms.api.model.service.IUpmsSystemService;
import com.rock.upms.client.util.UserUtil;
import com.rock.upms.model.UpmsSystem;
import com.rock.upms.model.UpmsSystemExample;
import com.rock.upms.model.UpmsSystemExample.Criteria;
import com.rock.upms.web.common.config.WebConfig;

@Controller
@RequestMapping(value = "upms/system")
public class UpmsSystemController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(UpmsSystemController.class);
	@Autowired
	private IUpmsSystemService upmsSystemService;

	/**
	 * 功能： 列表初始化<br>
	 * 说明：点击菜单或链接进行列表初始化，初始化默认查询条件等<br>
	 * 请求方式：链接（GET）
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions(value = "upms:system:list")
	public String toList(Model model) {
		BaseQuery query = new BaseQuery(1, WebConfig.getPageSize(), true);// 构造函数按默认分页方式分页，从第一页开始
		model.addAttribute("query", query);
		return "upms/system/upmsSystem";
	}

	/**
	 * 功能：列表查询<br>
	 * 说明：<br>
	 * （1）列表初始化完成后显示初始化数据<br>
	 * （2）页面查询条件发生改变后的查询<br>
	 * 请求方式：ajax post请求
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions(value = "upms:system:list")
	public BaseResult list(BaseQuery query) {
		int pageNum = query.getPageNum();// 页号
		int pageSize = query.getPageSize();// 每页显示记录数
		int navigatePages = WebConfig.getPageNavigatePages();// 导航显示页数
		if (!query.getIsPagination()) {
			pageSize = 0;
		}
		// 封装查询条件（自动生成三个，请根据实际修改）
		String systemCode = (String) query.getExtend().get("systemCode");// 系统编号
		String systemName = (String) query.getExtend().get("systemName");// 系统名称
		// 机构名模糊匹配
		String orderBy = " id asc ";// 默认ID降序
		UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
		Criteria criteria = upmsSystemExample.or();
		if (StringUtil.isNotBlank(systemCode)) {
			criteria.andSystemCodeEqualTo(systemCode);
		}
		if (StringUtil.isNotBlank(systemName)) {
			criteria.andSystemNameLike("%" + systemName + "%");
		}
		upmsSystemExample.setOrderByClause(orderBy);
		PageInfo<UpmsSystem> page = upmsSystemService.selectByExampleForStartPage(upmsSystemExample, pageNum, pageSize, navigatePages);
		if (page != null) {
			List<UpmsSystem> upmsSystemList=page.getList();
			if(upmsSystemList!=null){
				int size =upmsSystemList.size();
				for(int i=0;i<size;i++){
					String isShow=upmsSystemList.get(i).getIsShow();
					if(isShow.equals("0")){
						upmsSystemList.get(i).setIsShow("不显示");
					}else if(isShow.equals("1")){
						upmsSystemList.get(i).setIsShow("显示");
					}
					String visual=upmsSystemList.get(i).getVisualType();
					if(visual.equals("0")){
						upmsSystemList.get(i).setVisualType("登录");
					}else if(visual.equals("1")){
						upmsSystemList.get(i).setVisualType("授权");
					}
					
				}
			}
			return BaseResult.success("").add("pageInfo", page);
		} else {
			return BaseResult.fail("获取数据失败");
		}
	}

	/**
	 * 功能：新增（初始化）<br>
	 * 请求方式：ajax get
	 * 
	 * @return Msg的json串
	 */
	@ResponseBody
	// @RequiresPermissions("upms:system:add")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public BaseResult toAdd() {
		UpmsSystem vo = new UpmsSystem();
		vo.setCreateName(UserUtil.getUsername());
		vo.setCreateTime(new Date());
		return BaseResult.success("").add("upmsSystem", vo);
	}

	/**
	 * 功能：新增（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:add")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public BaseResult add(UpmsSystem vo) {
		log.trace("--->add vo:" + vo.toString());
		int seqno = 1;
		// 获取seqno，取数据库seqno最大值加1，没有则为1
		UpmsSystemExample example = new UpmsSystemExample();
		example.setOrderByClause("  seqno desc ");
		UpmsSystem us = upmsSystemService.selectFirstByExample(example);
		if (us != null) {
			seqno = us.getSeqno() + 1;
		}
		vo.setSeqno(seqno);
		vo.setCreateName(UserUtil.getUsername());
		vo.setCreateTime(new Date());
		int result = upmsSystemService.insert(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：删除 <br>
	 * 说明：根据主键ID删除记录，ID未加密
	 * 
	 * @param cryptId
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public BaseResult delete(@PathVariable("id") String id) {
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			int backint = upmsSystemService.deleteByPrimaryKey(idint);
			if (backint != 0) {
				return BaseResult.success("删除成功");
			} else {
				return BaseResult.fail("删除失败");
			}
		}
	}

	/**
	 * 功能：修改（初始化）<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public BaseResult toEdit(@PathVariable("id") String id) {
		UpmsSystem vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsSystem) upmsSystemService.selectByPrimaryKey(idint);
		}
		return BaseResult.success("").add("upmsSystem", vo);
	}

	/**
	 * 功能：修改（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public BaseResult edit(UpmsSystem vo) {
		log.trace("--->edit vo:" + vo.toString());
		int result = upmsSystemService.updateByPrimaryKey(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：查看<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login") // 登录访问，方便测试，请据实修改
	// @RequiresPermissions("upms:system:read")
	@RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
	public BaseResult read(@PathVariable("id") String id) {
		UpmsSystem vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsSystem) upmsSystemService.selectByPrimaryKey(idint);
			vo.setId(null);// ID不回显到页面
		}
		return BaseResult.success("").add("upmsSystem", vo);
	}
	
	
}